package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.article.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        //1. 校验参数 文章id必须传
        Long articleId = dto.getArticleId();
//        if (articleId == null){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"文章不存在");
//        }
        //2. 根据登录用户id 或 设备id查询行为实体数据(阅读操作可不登录)
        ApUser user = AppThreadLocalUtils.getUser();
        Integer loginId = user.getId();
        Integer equipmentId = dto.getEquipmentId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(loginId, equipmentId);
        //3. 判断阅读行为是否存在
        String entryId = apBehaviorEntry.getId();
        Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
        ApReadBehavior queryResult = mongoTemplate.findOne(query, ApReadBehavior.class);
        if (queryResult != null){
            //4. 存在  将阅读行为的count字段加1 并修改
            queryResult.setCount((short) (queryResult.getCount()+1));
            queryResult.setCreatedTime(new Date());
            mongoTemplate.save(queryResult);
        }else {
            //5. 不存在  创建阅读行为 并初始化count字段值为 1
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setCount((short) 1);
            apReadBehavior.setArticleId(articleId);
            apReadBehavior.setEntryId(entryId);
            apReadBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apReadBehavior);
        }

        // 发送阅读行为消息，用于修改文章行为汇总数量 更改文章热度
        NewBehaviorDTO behaviorDTO = new NewBehaviorDTO();
        behaviorDTO.setType(NewBehaviorDTO.BehaviorType.VIEWS);
        behaviorDTO.setArticleId(dto.getArticleId());
        behaviorDTO.setAdd(1);
        rabbitTemplate.convertAndSend(
                HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE,
                behaviorDTO  // 如果是json序列化，直接发对象
                // 如果没有设置序列化，发送json字符串消息 JSON.toJSONString(behaviorDTO)
        );
        log.info("成功 发送 阅读行为消息   消息内容:{} ",behaviorDTO);

        return ResponseResult.okResult();
    }
}
